<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "channel".
 *
 * @property string $cid
 * @property string $cname
 * @property string $create_time
 * @property string $manager_id
 * @property string $update_time
 * @property string $fid
 * @property string $list_template
 * @property string $show_template
 * @property integer $display
 * @property integer $type
 * @property string $seq
 */
ini_set('max_input_nesting_level','100000');
ini_set('max_execution_time','100000');
class Channel extends \yii\db\ActiveRecord
{
    public $name_list;
    const  DISPLAY_YES=1; //1展示
    const  DISPLAY_NO=0;   //0为不展示
    const TYPE_LIST=1;//列表页
    const TYPE_SINGLE=2;//单页面
    public static $display_array=[
        self::DISPLAY_YES=>'展示',
        self::DISPLAY_NO=>'不展示'
    ];
    public static $type_array=[
        self::TYPE_LIST=>'列表页',
        self::TYPE_SINGLE=>'单页面',
    ];
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'channel';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['create_time', 'manager_id', 'update_time', 'fid', 'display', 'type', 'seq'], 'integer'],
            [['cname', 'list_template', 'show_template'], 'string', 'max' => 128],
            [['cname'],'required','message'=>'频道不能为空'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'cid' => 'Cid',
            'cname' => '频道名称',
            'create_time' => '创建时间',
            'name_list'=>'频道二级频道',
            'manager_id' => 'Manager ID',
            'update_time' => '更新时间',
            'fid' => '父类',
            'list_template' => '列表模板',
            'show_template' => '内容页模板',
            'display' => '展现方式',
            'type' => '类型',
            'seq' => '排序',
        ];
    }
    public function save($runValidation = true, $attributeNames = null){
        $arr=Yii::$app->request->post();
        if($this->isNewRecord){
            $this->create_time=$this->update_time=time();
            $this->manager_id=yii::$app->session->get('uid');
        }else{
            $this->update_time=time();
        }
        $this->seq=intval($this->seq);
        return parent::save($runValidation, $attributeNames);
    }
    public function saveChannel($id,$runValidation = true, $attributeNames = null){
            if($this->isNewRecord){
                $channel=self::find()->where(['cid'=>$id])->asArray()->one();
                $this->list_template=$channel['list_template'];
                $this->manager_id=yii::$app->session->get('uid');
                $this->show_template=$channel['show_template'];
                $this->fid=$id;
                $this->display=$channel['display'];
                $this->type=$channel['type'];
                $this->create_time=$this->update_time=time();
            }

    }
    public function  saveChannels($id,$runValidation = true, $attributeNames = null){
        $channel=self::find()->where(['cid'=>$id])->asArray()->one();
        $this->list_template=$channel['list_template'];
        $this->show_template=$channel['show_template'];
        $this->display=$channel['display'];
        $this->type=$channel['type'];
        return parent::save($runValidation, $attributeNames);
    }
    //获取正则匹配到的list模板
    public function readDir(){
        $path=Yii::getAlias('@app').'/admin/views/channel/';
        $url1=$this->readDirectory($path);
        $listArray=array();
        foreach($url1 as $url){
            if($this->type==self::TYPE_SINGLE){
                if($this->getUrl3($url)){
                    $listArray[$url] = $url;
                }
            } else {
                if($this->getUrl($url)){
                    $listArray[$url] = $url;
                }
            }

        }

        return $listArray;
    }
    //获取正则匹配到的show模板
    public function readShow(){
        $path=Yii::getAlias('@app').'/admin/views/channel/';
        $url1=$this->readDirectory($path);
        $showArray=array();
        foreach($url1 as $url){
            if($this->getUrl2($url)){
                $showArray[$url] = $url;
            }
        }

        return $showArray;
    }
    //获取正则匹配到的Single模板
    public function readSingle(){
        $path=Yii::getAlias('@app').'/admin/views/channel/';
        $url1=$this->readDirectory($path);
        $showArray=array();
        foreach($url1 as $url){
            if($this->getUrl3($url)){
                $showArray[$url] = $url;
            }
        }

        return $showArray;
    }
    //循环读取目录
    public  function readDirectory($path) {

        // 打开目录句
        $handle = opendir ( $path );
        $files=array();
        // 循环读取目录条目
        while ( $item = readdir ( $handle ) ) {
            if ($item != '.' && $item != '..') {
                if (is_file ( $path . '/' . $item )) {
                    $files["$item"]=$item;
                } else {
                    echo $item;
                    $this->readDirectory ( $path . '/' . $item );
                }
            }
        }

        // 关闭目录句柄

        closedir ( $handle );
        return $files;
    }
    //匹配列表的模板
    public function getUrl($url) {
        $rule = "/^list_(.*?)\.php/i";
        return preg_match($rule,$url);
    }
    //匹配show的模板
    public function getUrl2($url) {
        $rule = "/^show_(.*?)\.php/i";
        return preg_match($rule,$url);
    }
    //匹配single的模板
    public function getUrl3($url) {
        $rule = "/^single_(.*?)\.php/i";
        return preg_match($rule,$url);
    }
    public static function getListChannelArray(){
        $channels = self::find()->where(['fid'=>0])->all();
        $return = array();
        foreach($channels as $one){
            $return[$one->cid] = $one->cname;
        }
        return $return;
    }
    public function getDate($time){
        return date("Y-m-d H:i:s",$time);
    }
    public function getChannel_list(){
          $data=self::find()->where(['fid'=>0])->asArray()->indexBy('cid')->all();
          $return=array();
          foreach($data as $channel_id=>$one){
               $data[$channel_id]=$data[$channel_id]['cname'];
               $data[$channel_id][$one['cid']]=$one->cname;
          }
        return $return;

    }
    public static $reStr = [];
    public function getChannel_str($fid=0,$num=0){
        //找符合$fid的分类
        $arr=self::find()->where(['fid'=>$fid])->asArray()->all();
        //自己调用自己
        $spaceStr=str_repeat("-", $num*2);
        foreach($arr as $v){
            self::$reStr[$v['cid']]=$spaceStr.$v['cname'];
            $this->getChannel_str($v['cid'],++$num);
            $num--;
        }
        return self::$reStr;
    }
    public static $str="";
    public function getChannel($cid,$fid=0,$num=0){
        //找符合$fid的分类
        $arr=self::find()->where(['fid'=>$fid])->asArray()->all();
        //自己调用自己
        $spaceStr=str_repeat("-", $num*2);
        foreach($arr as $v){
            $selected=$cid==$v['cid']?"selected='selected'":"";
            self::$str .="<option value='{$v['cid']}' ".$selected.">".$spaceStr.$v['cname']."</option>";
            $this->getChannel($cid,$v['cid'],++$num);
            $num--;
        }
        return self::$str;
    }
}
